package com.gov.dna.common.util;

import org.apache.commons.lang3.StringUtils;

/**
 * 字符串工具类
 * @author (作者) Dong Jie 154046519@qq.com
 * @version V1.0
 * @date 2018/12/5 16:39
 * @since (该版本支持的JDK版本) 1.8
 */
public class GovStringUtils {
	private static  final String COMMA=",";
	private static  final String DOUBLE_COMMA=",,";

	/**
	 * 驼峰转为下划线
	 * @author (作者) Dong Jie 154046519@qq.com
	 * @param camelCaseName
	 * @return java.lang.String
	 * @date 2018/12/5 17:25
	 */
	public static String underscoreName(String camelCaseName) {
		StringBuilder result = new StringBuilder();
		if (camelCaseName != null && camelCaseName.length() > 0) {
			result.append(camelCaseName.substring(0, 1).toLowerCase());
			for (int i = 1; i < camelCaseName.length(); i++) {
				char ch = camelCaseName.charAt(i);
				if (Character.isUpperCase(ch)) {
					result.append("_");
					result.append(Character.toLowerCase(ch));
				} else {
					result.append(ch);
				}
			}
		}
		return result.toString();
	}

	/**
	 * 下划线转化为驼峰
	 * @author (作者) Dong Jie 154046519@qq.com
	 * @param underscoreName
	 * @return java.lang.String
	 * @date 2018/12/5 17:26
	 */
	public static String camelCaseName(String underscoreName) {
		StringBuilder result = new StringBuilder();
		if (underscoreName != null && underscoreName.length() > 0) {
			boolean flag = false;
			for (int i = 0; i < underscoreName.length(); i++) {
				char ch = underscoreName.charAt(i);
				if ("_".charAt(0) == ch) {
					flag = true;
				} else {
					if (flag) {
						result.append(Character.toUpperCase(ch));
						flag = false;
					} else {
						result.append(ch);
					}
				}
			}
		}
		return result.toString();
	}


	/**
	 * 去掉两头的逗号
	 * @author (作者) Dong Jie 154046519@qq.com
	 * @param str
	 * @return java.lang.String
	 * @date 2018/12/7 10:03
	 */
	public static String removeBothEndsComma(String str){
		if (str == null || str == ""){
			return "";
		}
		if(str.contains(DOUBLE_COMMA)){
			str=str.replaceAll(",+", ",");
		}
		if(str.startsWith(COMMA)){
			str=str.substring(1,str.length());
		}
		if(str.endsWith(COMMA)){
			str=str.substring(0,str.length()-1);
		}
		return str;
	}

	/**
	 * 在逗号分隔的每个字符串添加前缀后缀
	 * @author (作者) Dong Jie 154046519@qq.com
	 * @param str
	 * @param start 前缀
	 * @param end 后缀
	 * @return java.lang.String
	 * @date 2018/12/7 10:06
	 */
	public static String addBothEndsString(String str,String start,String end){
		if (str == null || str == ""){
			return "";
		}
		if(str.contains(DOUBLE_COMMA)){
			str=str.replaceAll(",+", ",");
		}
		StringBuffer sb=new StringBuffer();
		String [] ss=str.split(",");
		for(String s:ss){
			if(StringUtils.isNotEmpty(s)){
				sb.append(start).append(s).append(end).append(",");
			}
		}
		return removeBothEndsComma(sb.toString());
	}

	/**
	 * 两边加逗号
	 * @author (作者) Dong Jie 154046519@qq.com
	 * @param str
	 * @return java.lang.String
	 * @date 2018/12/7 10:07
	 */
	public static String addBothEndsComma(String str){
		if (StringUtils.isEmpty(str)){
			return "";
		}
		if(str.contains(DOUBLE_COMMA)){
			str=str.replaceAll(",+", ",");
		}
		if(!str.startsWith(COMMA)){
			str=","+str;
		}
		if(!str.endsWith(COMMA)){
			str=str+",";
		}
		return str;
	}

}
